package com.luohh.treeroom.session;

import com.luohh.treeroom.dto.BaseErrResult;
import com.luohh.treeroom.entity.UserInfo;
import com.luohh.treeroom.exception.CommonError;
import com.luohh.treeroom.redis.RedisParam;
import com.luohh.treeroom.util.JsonObjUtils;
import com.luohh.treeroom.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class SessionService {

    @Autowired
    HttpServletRequest request;
    @Autowired
    RedisTemplate<String, String> redisTemplate;

    /**
     * 读取缓存的userId
     */
    public Integer getUserId() {
//         return 1;
        String token = request.getHeader("token");
        if (StringUtil.isBlank(token)) {
            return null;
//             CommonError.CommonErr(new BaseErrResult("-1", "Token为空，请登录"));
        }

        String userIdStr = null;
        try {
            userIdStr = redisTemplate.opsForValue().get(RedisParam.USER_ID + token);
            if (StringUtil.isNotBlank(userIdStr)) {
                String cacheToken = redisTemplate.opsForValue().get(RedisParam.USER_TOKEN + userIdStr);
                if (StringUtil.isBlank(cacheToken)) {
                    CommonError.CommonErr(new BaseErrResult("-99", "Token失效，请重新登录"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            CommonError.CommonErr(new BaseErrResult("-99", "Redis异常"));
        }
        if (StringUtil.isBlank(userIdStr)) {
            CommonError.CommonErr(new BaseErrResult("-99", "登录超时，请重新登录"));
        }
        return Integer.valueOf(userIdStr);
    }

    /**
     * 读取缓存的userInfo
     */
    public UserInfo getUserInfo() {
        String token = request.getHeader("token");
        if (StringUtil.isBlank(token)) {
            // return null;
            CommonError.CommonErr(new BaseErrResult("-99", "Token为空，请登录"));
        }
        String userInfoStr = null;
        try {
            userInfoStr = redisTemplate.opsForValue().get(RedisParam.USER_INFO + token);
        } catch (Exception e) {
            e.printStackTrace();
            CommonError.CommonErr(new BaseErrResult("-99", "Redis异常"));
        }
        if (StringUtil.isBlank(userInfoStr)) {
            CommonError.CommonErr(new BaseErrResult("-99", "登录超时，请重新登录"));
        }
        UserInfo user = null;
        try {
            user = JsonObjUtils.str2obj(userInfoStr, UserInfo.class);
        } catch (Exception e) {
            log.error("userInfo解析异常", e);
            CommonError.CommonErr(new BaseErrResult("-99", "userInfo解析异常"));
        }

        return user;
    }

    public void updateRedisExpire() {
        String token = request.getHeader("token");
        Integer userId = this.getUserId();
        redisTemplate.expire(RedisParam.USER_INFO + token, 2, TimeUnit.DAYS);
        redisTemplate.expire(RedisParam.USER_ID + token, 2, TimeUnit.DAYS);
        if (userId != null) {
            redisTemplate.expire(RedisParam.USER_TOKEN + userId, 2, TimeUnit.DAYS);
        }
    }

}
